#!/usr/local/bin/python

from __future__ import print_function
import requests
import json
import base64
import sys
import re

def usage():
    print('usage: ' + sys.argv[0] + ' <github domain> <github username> <github oauth token>')

if len(sys.argv) != 4:
    usage()
    exit()

protocol = 'https://'
baseUrl = '/api/v3/'
domain = sys.argv[1]
githubUrl = protocol + domain + baseUrl
repoUrl = githubUrl + 'repos/'
auth = (sys.argv[2], sys.argv[3])

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

def getAtlasVersion(repo, tag, filePaths):
    for filePath in filePaths:
        versionFile = requestFile(repo, tag, filePath)
        if (versionFile.status_code == requests.codes.ok):
            versionString = parseAtlasVersion(versionFile)
            if versionString is not None:
                return repo + '-' + tag + ': ' + versionString
        else:
            eprint(repo + ': no version file found at ' + filePath)

    return repo + '-' + tag + ': no AtlasDB version found'

def parseAtlasVersion(res):
    jsonRes = json.loads(res.text)
    decodedContent = base64.b64decode(jsonRes['content'])
    match = re.search('atlasVersion|atlasDbVersion|com.palantir.atlasdb', decodedContent)
    if match == None:
        eprint("Couldn't find version in <" + decodedContent + ">")
        return
    return decodedContent[match.start():].splitlines()[0]

def requestFile(repo, tag, filePath):
    return requests.get(repoUrl + repo + '/contents/' + filePath + '?ref=' + tag, auth=auth);

def getReleases(repo):
    res = requests.get(repoUrl + repo + '/releases', auth=auth)
    jsonRes = json.loads(res.text)
    if len(jsonRes) == 0:
        print(repo + ': has no releases')
    return jsonRes

def getProducts(searchTerms):
    res = requests.get(githubUrl + 'search/code?q=' + searchTerms + '&per_page=100', auth=auth);
    jsonRes = json.loads(res.text)
    print('Found ' + str(jsonRes['total_count']) + ' repos referencing ' + searchTerms);
    repos = []
    for searchResult in jsonRes['items']:
        repos.append(searchResult['repository']['full_name'])
    return repos

print('Fetching products using AtlasDB')

products = frozenset(getProducts('atlas+extension:properties') + getProducts('atlasdb+extension:props'))

print('Found the following products:')
for product in products:
    print(product)

print('Fetching AtlasDB versions:')

for productInfo in products:
    for release in getReleases(productInfo):
        print(getAtlasVersion(productInfo, release['tag_name'], ['gradle.properties', 'versions.props']))
